package cn.wzptsoft.mall.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.aspect.AutoFull;
import cn.wzptsoft.mall.aspect.OperationType;
import cn.wzptsoft.mall.mapper.OrderMapper;
import cn.wzptsoft.mall.pojo.*;
import cn.wzptsoft.mall.pojo.dto.PutOrderDTO;
import cn.wzptsoft.mall.pojo.vo.AddressInfoVO;
import cn.wzptsoft.mall.pojo.vo.OrderInfoVO;
import cn.wzptsoft.mall.pojo.vo.PageVo;
import cn.wzptsoft.mall.service.IOrderItemService;
import cn.wzptsoft.mall.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class IOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private IOrderItemService orderItemService;

    @Override
    @Transactional
    public void add(PutOrderDTO putOrderDTO) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        int id = user.getUser().getId();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        Order order = Order.builder()
                .userId(id)
                .orderNo(now.format(formatter))
                .totalAmount(putOrderDTO.getTotalAmount())
                .totalCount(putOrderDTO.getTotalCount())
                .snapAddress(
                        SnapAddressInfo.builder()
                                .tel(putOrderDTO.getTel())
                                .detail(putOrderDTO.getSnapAddress())
                                .build()
                )
                .status(1)
                .build();
        save(order);
        Long orderId = order.getId();

        orderItemService.add(orderId, putOrderDTO.getSpuInfo());
    }

    @Override
    public PageVo<OrderInfoVO> getDetail(Integer start, Integer count) {
        //分页条件
        Page<Order> page = Page.of(start, count);
        //获取id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Integer id = ((LoginUser) authentication.getPrincipal()).getUser().getId();
        //根据分页条件和id信息，查询order数据
        Page<OrderInfoVO> list = baseMapper.getInfo(page, id);
        //获取order的ids，并查询每条id的orderitem
        Map<Integer, List<OrderInfoVO.OrderInfoVoItem>> items = orderItemService
                .getOrderInfoVoItemByIds(list.getRecords().stream().map(OrderInfoVO::getId).toList());
        //将orderitem的数据放到对应的order中
        list.getRecords().forEach(i -> i.setItem(items.get(i.getId())));
        //封装pagevo对象
        PageVo<OrderInfoVO> vo = PageVo.<OrderInfoVO>builder()
                .total(list.getTotal())
                .pages(list.getPages())
                .list(list.getRecords())
                .build();
        return vo;
    }
}
